/**
 * Copyright (c) 2003-2017 Ben Lau
 * All rights reserved.
 */
package lock;

import java.util.concurrent.locks.Lock;

/**
 * @author liubin
 *
 */
public class TwinsLockTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		final Lock lock = new TwinsLock();
		class Worker extends Thread {
			@Override
			public void run() {
				while (true) {
					lock.lock();
					try {
						Thread.sleep(1000);
						System.out.println(Thread.currentThread().getName());
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					} finally {
						lock.unlock();
					}
				}

			}
		}

		// 启动10个线程
		for (int i = 0; i < 10; i++) {
			Worker worker = new Worker();
			worker.setDaemon(true);
			worker.start();
		}

		// 每隔一秒换行
		for (int i = 0; i < 10; i++) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println();
		}
	}

}
